﻿using UnityEngine;

public class RadiusUpgradeBtn : UpgradeBtn
{
    void Start()
    {
        InitUI();
    }
    void InitUI()
    {
        int nextLv = gameUser.followersNum + 1;
        this.lvText.text = "Level " + nextLv;
        int price = MLocalUserData.GetUpgradePrice(nextLv);
        this.priceText.text = price.ToString();

        priceText.transform.parent.gameObject.SetActive(price <= gameUser.coin);
        mask.SetActive(price > gameUser.coin);
    }

    public override void OnUpgradeJumpPower()
    {  
        int nextLv = gameUser.followersNum + 1;
        int price = MLocalUserData.GetUpgradePrice(nextLv);

        if (gameUser.coin >= price)
        {
            OnUpgrade();
        }
        else
        {
            AudioController.instance.PauseMusic();

            Game.Instance.adManager.PlayVideoAd(() =>
                {
                    AudioController.instance.ResumeMusic();
                    OnUpgrade(true);
                }
                , () =>
                {
                    AudioController.instance.ResumeMusic();
                }, (err) =>
                {
                    AudioController.instance.ResumeMusic();
                    Game.Instance.ShowNoticePanel(err);
                });
        }

    }

    void OnUpgrade(bool ad = false)
    {
        int oldLv = gameUser.followersNum;
        int newLv = gameUser.UpgradeEnduranceLevel(ad);
        if (newLv > oldLv)
        {
            InitUI();

            float newAddJP = MLocalUserData.GetEnduranceWithLevel(gameUser.followersNum);

            MBattleMainUI battleUI = GetComponentInParent<MBattleMainUI>();

            UnityEngine.GameObject hitFXPref =
                ResourceManager.Load<UnityEngine.GameObject>(GameResourceManager.upgradeFxPath);

            if (hitFXPref)
            {
                var fx = UnityEngine.GameObject.Instantiate(hitFXPref);
                fx.AddComponent<DelayDestroy>();
                fx.transform.position = battleUI.MBattleController.me.transform.position + new Vector3(0, 0.15f, 0);
            }

            //battleUI.MBattleController.me.UpdateEndurance(newAddJP);
        }
    }  
    
    void Update()
    {
        if (Time.frameCount % 60 == 0)
        {
            InitUI();
        }
    }
}